package org.lib.rabbitmqhello.try4;

import java.util.List;
import java.util.function.Consumer;

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import lombok.extern.slf4j.Slf4j;

/**
 * 1、同一个队列的@RabbitListener，只有一个配置为 exclusive=true 的生效
 * @author ben
 * @date 2021-11-07 17:25:20 CST
 */
@Component
@Slf4j
public class Try4Listener {

	private static Consumer<Object> cs = System.out::println;
	
//	@RabbitListener(queues = {RabbitConfig4.QUEUE_4}, exclusive = true)
	public void processMessage1(String content) {
		cs.accept("1 content=" + content);
	}

	// 无论 exclusive 配置为 什么，都不会再队列生成 consumer
//	@RabbitListener(queues = {RabbitConfig4.QUEUE_4}, exclusive = true)
//	@RabbitListener(queues = {RabbitConfig4.QUEUE_4}, exclusive = false)
	public void processMessage2(String content) {
		cs.accept("2 content=" + content);
	}

//	@RabbitListener(queues = {RabbitConfig4.QUEUE_4}, exclusive = true)
//	@RabbitListener(queues = {RabbitConfig4.QUEUE_4})
	public void processMessage3(List<String> contentList) {
		cs.accept("3 contentList.size=" + contentList.size());
		if (!CollectionUtils.isEmpty(contentList)) {
			contentList.forEach(content->{
				cs.accept("3 item=" + content);
			});
			cs.accept("---");
		} else {
			cs.accept("contentList is empty");
		}
	}

	/**
	 * 批量消费
	 * @author ben
	 * @date 2021-11-07 18:45:32 CST
	 * @param msgs
	 */
	@RabbitListener(queues = {RabbitConfig4.QUEUE_4}, containerFactory = "batchRabbitListenerContainerFactory4")
	public void processMessage4(List<Message> msgs) {
		cs.accept("3 msgs.size=" + msgs.size());
		if (!CollectionUtils.isEmpty(msgs)) {
			msgs.forEach(msg->{
				Object payload = msg.getPayload();
				cs.accept("3 msg=" + msg + ", " + payload);
			});
			cs.accept("---");
		} else {
			cs.accept("msgs is empty");
		}
	}
	
}
